package com.mar.tech.unify.verticle.info;

import com.mar.tech.unify.RouterVerticle;
import com.mar.tech.unify.bean.ApiHandler;
import com.mar.tech.unify.bean.Result;
import com.mar.tech.unify.constants.QueueName;
import com.mar.tech.unify.service.database.DatabaseService;
import com.mar.tech.unify.verticle.BaseVerticle;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.web.RoutingContext;

/**
 * Description: 基本信息 .<br>
 *
 * @author m-xy
 * @since 2019/9/4 13:54
 */
public class InfoVerticle extends BaseVerticle implements RouterVerticle {
  private final String TEST = "test";

  @Override
  public void start() throws Exception {
    super.start();
    databaseService = DatabaseService.createProxy(vertx, QueueName.DATABASE_QUEUE);
    ROUTERS.add(new ApiHandler(HttpMethod.GET, "/info", this::queryInfo));
    ROUTERS.add(new ApiHandler(HttpMethod.GET, "/test", this::test));
    ROUTERS.add(new ApiHandler(HttpMethod.GET, "/db/info", this::queryInfoByDb));
  }

  private void test(RoutingContext routingContext) {
    routingContext.response().end(TEST);
  }

  private void queryInfo(RoutingContext routingContext) {
    Result.ok(routingContext, "Luckin Dog!!");
  }

  private void queryInfoByDb(RoutingContext routingContext) {
    JsonArray param = new JsonArray();
    param.add(routingContext.request().getParam("id"));
    queryFuture("queryInfo", param).onComplete(reply -> {
      if (reply.succeeded()) {
        Result.ok(routingContext, reply.result());
      } else {
        routingContext.fail(reply.cause());
      }
    });
  }
}
